﻿using System;
using System.IO;
using System.Windows.Forms;
using OccupierArchiver.DataRepository;
using OccupierArchiver.Helpers;
using OccupierArchiver.UserInterface;

namespace OccupierArchiver
{
    static class Program
    {
        /// <summary>
        /// Specify the data directory of the application.
        /// </summary>
        internal static string DataDir { get; set; }

        /// <summary>
        /// Specify the temporary directory of the application.
        /// </summary>
        internal static string TempDir { get; set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            TempDir = Path.GetTempPath() + Application.ProductName;
            if (!Directory.Exists(TempDir)) Directory.CreateDirectory(TempDir);
            SettingsManager.CheckSettings();
            DataTables.InitializeTables();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormMain());

            DataTables.UninitializeTables();
            SettingsManager.SaveSettings();
            // CleanTempDir();
        }

        /// <summary>
        /// Clean the temporary directory of the application.
        /// </summary>
        static void CleanTempDir()
        {
            bool flag = false;

            while (flag == false)
            {
                try
                {
                    if (Directory.Exists(TempDir))
                        Directory.Delete(TempDir, true);

                    flag = true;
                }
                catch (Exception)
                {
                    flag = false;
                }
            }
        }
    }
}
